<template>
    <view class="micro-temp">
        <!-- #ifndef H5 -->
        <web-view
            class="webView"
            :webview-styles="webviewStyles"
            ref="webView"
            :src="url"
            @message="reciveMessage"></web-view>
        <!-- #endif -->
        <!-- #ifdef H5 -->
        <iframe class="main" :height="listHeight" id="iframeContent" :src="url" />
        <!-- #endif -->
    </view>
</template>

<script>
import storage from "@/uni-sub-base/utils/storage.js"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
export default {
    props: {
        url: {
            type: String,
            default: "",
        },
    },
    data() {
        return {
            listHeight: 0,
            history: 0,
            webviewStyles: {
                progress: false,
            },
        }
    },
    mounted() {
        //#ifdef H5
        this.getListHeight()
        //#endif
        // #ifdef APP-PLUS
        let defaultSite = storage.getCache("DefaultSite")
        if (defaultSite && uni.getSystemInfoSync().platform == "ios") {
            const currentWebview = this.getWebView()
            let that = this
            setTimeout(() => {
                uni.getSystemInfo({
                    //调用uni-app接口获取屏幕高度
                    success(res) {
                        //成功回调函数
                        let statusBarHeight = res.statusBarHeight
                        let ph = res.windowHeight
                        let height = ph - statusBarHeight
                        currentWebview.setStyle({
                            top: statusBarHeight,
                            height: height,
                        })
                    },
                })
                storage.removeCache("DefaultSite")
            }, 2000) //如果是页面初始化调用时，需要延时一下
        }

        // #endif
    },
    onLoad(e) {},
    onShow() {},
    methods: {
        //获取列表高度
        getListHeight() {
            //获取高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
            })
        },
        reciveMessage(e) {
            console.log(e)
            console.log(e.detail.data[0])
            try {
                if (e.detail.data[0]) {
                    if (e.detail.data[0].path) {
                        let path = e.detail.data[0].path
                        if (path.includes("/pages/product/index")) {
                            // let path.rep
                            let productTagId = path.split("=")[1]
                            console.log(productTagId)
                            storage.setCache("productTagId", productTagId)
                            this.$nextTick(() => {
                                uni.switchTab({
                                    url: "/pages/product/index",
                                })
                            })
                        } else {
                            this.easyGoTo(path)
                        }
                    } else if (e.detail.data[0].action) {
                        let action = e.detail.data[0].action
                        if (action == "logout") {
                            //退出登录
                            storage.removeCache("token")
                            uni.reLaunch({
                                url: "/pages/login/index",
                            })
                        }
                    }
                }
            } catch (e) {
                //TODO handle the exception
                console.log(e)
            }
        },
        getWebView() {
            return this.$parent.$scope.$getAppWebview().children()[0]
        },
    },
}
</script>

<style lang="scss">
.main {
    position: absolute;
    display: block;
    border: 0px;
    width: 100%;
}
</style>
